package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.common.BaseResponse;
import com.example.yaja.yaja.common.ResponseBuilder;
import com.example.yaja.yaja.dao.UserMapper;
import com.example.yaja.yaja.entity.User;
import com.example.yaja.yaja.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * Created by Zero on 2017/12/4.
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;


    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public int login(@RequestBody User user, HttpSession userName) {

        int result = 0;
        String username = user.getUsername();
        String password = user.getPassword();
        User user1 = userService.findUserByUsername(username);
        if (user != null && user1 != null) {
            int userId = user1.getUserId();
            if (user1.getState() == 0) {
                result = 2;
                return result;
            }
            if (user1.getUsername().equals(username) && user1.getPassword().equals(password)) {
                userName.setAttribute("username", username);
                userName.setAttribute("userId", userId);
                result = 1;
            } else {
                result = 0;
            }
        } else {
            result = 0;
        }
        return result;
    }


    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public int addUser(@RequestBody User user) {

        int result = 0;
        String username = user.getUsername();
        User user1 = userService.findUserByUsername(username);
        if (user1 == null) {
            userService.addUser(user);
            result = 1;
        } else {
            result = 0;
        }
        return result;
    }

    /**
     * 查询所有用户
     *
     * @param currPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/queryUserList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse queryUserList(@RequestParam Integer currPage,
                                      @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();
        List<User> userList = userMapper.queryUserList();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            if (userList != null) {
                custom.data(userList).currPage(currPage).pageSize(pageSize).totalCount(userList.size());
            } else {
                custom.failed("查询失败", -1);
            }
        } catch (Exception e) {
            custom.failed("查询失败", -1);
        }
        return custom.build();
    }

    /**
     * 更新用户状态---锁定用户不能登录
     *
     * @param userId
     * @param state
     * @return
     */
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateState(Integer userId, Integer state) {
        ResponseBuilder custom = ResponseBuilder.custom();
        int result = userService.updateUserState(userId, state);

        if (result != 0) {
            custom.success("成功", 0);
        } else {
            custom.failed("失败", -1);
        }
        return custom.build();
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse delUser(Integer userId) {
        ResponseBuilder custom = ResponseBuilder.custom();
        int result = userMapper.deleteByPrimaryKey(userId);

        if (result != 0) {
            custom.success("成功", 0);
        } else {
            custom.failed("失败", -1);
        }
        return custom.build();
    }

}
